<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="context-path" content="${pageContext.request.contextPath}">
    <title>后台管理系统 - 商品管理</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        body {
            padding-top: 56px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        .sidebar {
            min-height: calc(100vh - 56px);
            position: fixed;
            top: 56px;
            left: 0;
            width: 200px;
            z-index: 100;
            padding: 20px 0;
            box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
            background-color: #343a40;
        }
        .sidebar .nav-link {
            color: #ddd;
            padding: 0.5rem 1rem;
        }
        .sidebar .nav-link:hover {
            color: #fff;
            background-color: rgba(255, 255, 255, 0.1);
        }
        .sidebar .nav-link.active {
            color: #fff;
            background-color: rgba(255, 255, 255, 0.2);
        }
        .sidebar .nav-link i {
            margin-right: 8px;
        }
        .main-content {
            margin-left: 200px;
            padding: 20px;
            flex: 1;
        }
        .product-image-wrapper {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 4px;
            overflow: hidden;
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
        }
        .product-image-wrapper img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
        }
        .table-actions {
            min-width: 200px;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
        <a class="navbar-brand" href="${pageContext.request.contextPath}/admin">
            <i class="fas fa-dumbbell"></i> 体育商城后台
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="${pageContext.request.contextPath}/" target="_blank">
                        <i class="fas fa-external-link-alt"></i> 访问前台
                    </a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-toggle="dropdown">
                        <i class="fas fa-user-circle"></i> ${adminUser}
                    </a>
                    <div class="dropdown-menu dropdown-menu-right">
                        <a class="dropdown-item" href="${pageContext.request.contextPath}/admin/logout">
                            <i class="fas fa-sign-out-alt"></i> 退出登录
                        </a>
                    </div>
                </li>
            </ul>
        </div>
    </nav>

    <!-- 侧边栏 -->
    <jsp:include page="sidebar.jsp" />

    <!-- 主要内容 -->
    <div class="main-content">
        <div class="container-fluid">
            <!-- 标题行 -->
            <div class="row mb-4">
                <div class="col-md-6">
                    <h2 class="mb-1">商品管理</h2>
                    <p class="text-muted">管理所有体育用品商品</p>
                </div>
                <div class="col-md-6 text-right">
                    <button class="btn btn-primary" data-toggle="modal" data-target="#productModal">
                        <i class="fas fa-plus"></i> 添加商品
                    </button>
                </div>
            </div>
            
            <!-- 搜索过滤器 -->
            <div class="row mb-4">
                <div class="col-md-12">
                    <div class="card shadow-sm">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="searchName">商品名称</label>
                                        <input type="text" class="form-control" id="searchName" placeholder="输入商品名称">
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="searchCategory">商品分类</label>
                                        <select class="form-control" id="searchCategory">
                                            <option value="">全部分类</option>
                                            <c:forEach items="${categories}" var="category">
                                                <option value="${category.id}">${category.name}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="searchStatus">商品状态</label>
                                        <select class="form-control" id="searchStatus">
                                            <option value="">全部状态</option>
                                            <option value="1">上架</option>
                                            <option value="0">下架</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3 d-flex align-items-end">
                                    <button class="btn btn-primary btn-block" id="searchBtn">
                                        <i class="fas fa-search"></i> 搜索
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 商品列表 -->
            <div class="row">
                <div class="col-md-12">
                    <div class="card shadow-sm">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>图片</th>
                                            <th>名称</th>
                                            <th>价格</th>
                                            <th>库存</th>
                                            <th>分类</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="productTableBody">
                                        <tr>
                                            <td colspan="8" class="text-center">正在加载数据...</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            
                            <!-- 分页 -->
                            <nav aria-label="Page navigation" class="mt-4">
                                <ul class="pagination justify-content-center" id="pagination">
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 商品表单模态框 -->
    <div class="modal fade" id="productModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="productModalTitle">添加商品</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="productForm">
                        <input type="hidden" id="productId">
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="productName">商品名称 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="productName" required>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="productCategory">商品分类 <span class="text-danger">*</span></label>
                                <select class="form-control" id="productCategory" required>
                                    <option value="">选择分类</option>
                                    <c:forEach items="${categories}" var="category">
                                        <option value="${category.id}">${category.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="productPrice">价格 <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">￥</span>
                                    </div>
                                    <input type="number" class="form-control" id="productPrice" min="0.01" step="0.01" required>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="productStock">库存 <span class="text-danger">*</span></label>
                                <input type="number" class="form-control" id="productStock" min="0" required>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-12">
                                <label for="productSubtitle">副标题</label>
                                <input type="text" class="form-control" id="productSubtitle">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="productImage">主图</label>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="productImage" readonly>
                                    <div class="input-group-append">
                                        <button class="btn btn-outline-secondary" type="button" id="uploadImageBtn">上传</button>
                                    </div>
                                </div>
                                <div id="previewContainer" class="mt-2" style="display: none;">
                                    <img id="imagePreview" class="img-thumbnail" style="max-height: 150px;">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label>商品特性</label>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="productIsHot">
                                    <label class="form-check-label" for="productIsHot">热门商品</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="productIsNew">
                                    <label class="form-check-label" for="productIsNew">新品</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="productStatus" checked>
                                    <label class="form-check-label" for="productStatus">上架</label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="productDetail">商品详情</label>
                            <textarea class="form-control" id="productDetail" rows="5"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="saveProductBtn">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 图片上传模态框 -->
    <div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">上传图片</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="uploadForm" enctype="multipart/form-data">
                        <div class="form-group">
                            <label for="fileInput">选择图片</label>
                            <input type="file" class="form-control-file" id="fileInput" name="file" accept="image/*" required>
                            <small class="form-text text-muted">推荐尺寸: 800x800 像素，最大文件大小: 2MB</small>
                        </div>
                        <div id="uploadPreviewContainer" class="mt-3 d-none">
                            <img id="uploadPreview" class="img-thumbnail" style="max-width: 100%; max-height: 200px;">
                        </div>
                    </form>
                    <div class="progress mt-3 d-none" id="uploadProgress">
                        <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="confirmUploadBtn">上传</button>
                </div>
            </div>
        </div>
    </div>

    <!-- jQuery 和 Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- 初始化分类数据 -->
    <script>
        // 预先初始化分类数据
        var categoriesString = '[<c:forEach items="${categories}" var="category" varStatus="status">{"id": ${category.id}, "name": "${category.name}"}<c:if test="${!status.last}">,</c:if></c:forEach>]';
        window.categories = JSON.parse(categoriesString);
    </script>
    
    <script src="${pageContext.request.contextPath}/static/js/admin/product.js"></script>
</body>
</html> 